package org.dromara.business.controller;

import java.math.BigDecimal;
import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.business.domain.vo.WalletChangeVo;
import org.dromara.business.domain.bo.WalletChangeBo;
import org.dromara.business.service.WalletChangeService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 钱包变动记录
 *
 * @author zlj
 * @date 2024-08-20
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/change")
public class WalletChangeController extends BaseController {

    private final WalletChangeService walletChangeService;

    /**
     * 查询钱包变动记录列表
     */
    @SaCheckPermission("business:change:list")
    @GetMapping("/list")
    public TableDataInfo<WalletChangeVo> list(WalletChangeBo bo, PageQuery pageQuery) {
        return walletChangeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出钱包变动记录列表
     */
    @SaCheckPermission("business:change:export")
    @Log(title = "钱包变动记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WalletChangeBo bo, HttpServletResponse response) {
        List<WalletChangeVo> list = walletChangeService.queryList(bo);
        ExcelUtil.exportExcel(list, "钱包变动记录", WalletChangeVo.class, response);
    }

    /**
     * 获取钱包变动记录详细信息
     *
     * @param walletChangeId 主键
     */
    @SaCheckPermission("business:change:query")
    @GetMapping("/{walletChangeId}")
    public R<WalletChangeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long walletChangeId) {
        return R.ok(walletChangeService.queryById(walletChangeId));
    }

    /**
     * 新增钱包变动记录
     */
    @SaCheckPermission("business:change:add")
    @Log(title = "钱包变动记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WalletChangeBo bo) {
        return toAjax(walletChangeService.insertByBo(bo));
    }

    /**
     * 修改钱包变动记录
     */
    @SaCheckPermission("business:change:edit")
    @Log(title = "钱包变动记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WalletChangeBo bo) {
        return toAjax(walletChangeService.updateByBo(bo));
    }

    /**
     * 删除钱包变动记录
     *
     * @param walletChangeIds 主键串
     */
    @SaCheckPermission("business:change:remove")
    @Log(title = "钱包变动记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{walletChangeIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] walletChangeIds) {
        return toAjax(walletChangeService.deleteWithValidByIds(List.of(walletChangeIds), true));
    }

    /**
     *  用户发起提现
     * @param  userId 用户id
     * @param  money 变动金额(变动值,永远为正值)
     *
     * */
    @GetMapping("applyCash")
    public R<Void>   applyCash(@RequestParam("userId")String userId, @RequestParam("money") String money){
        walletChangeService.applyCash(userId,new BigDecimal(money));
        return R.ok();
    }
}
